package com.lz.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lz.blog.dao.mapper.ArticleMapper;
import com.lz.blog.dao.pojo.Article;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author Liz
 * @date 2023/02/20 15:00
 **/
@Component
public class ThreadService {


    //期望此操作在线程池执行，不影响主线程
    @Async("taskExecutor")
    public void updateArticleViewCount(ArticleMapper articleMapper, Article article) {
        try {
            int viewCounts = article.getViewCounts();
            Article article1 = new Article();
            System.out.println("更新完成。。。。。");
            article1.setViewCounts(viewCounts+1);
            LambdaUpdateWrapper<Article> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Article::getId,article.getId());
            //设置一个w为了在多线程下保证线程安全
            updateWrapper.eq(Article::getViewCounts,viewCounts);
            articleMapper.update(article1,updateWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
